var express = require('express');
var router = express.Router();
var query = require('../db/query')
var trans = require('../db/trans')
var db_user = require('../db/db_user')
var $ = require('../constant')
/* GET users listing. */
router.post('/login', function(req, res, next) {
    let username = req.body.username
    let password = req.body.password
    query(db_user.login,[username])
        .then(result => {
            if (result) {
                if(result[0].password == password) {
                    let user_id = result[0].id
                    let user = { id: user_id, username: result[0].username }
                    query(db_user.profile,[user_id]).then(result => {
                        user.profile = result[0]
                        return query(db_user.group, [user_id])
                    }).then(result => {
                        user.groups = result
                        return query(db_user.friends, [user_id])
                    }).then(result => {
                        user.friends = result
                        res.json({ code: $.CODE_SUCCESS, msg: $.MSG_LOGIN_SUCCESS, data: user })
                    }).catch(err => {
                        console.log(err)
                        res.json({ code: $.CODE_ERR_SYS, msg: $.MSG_ERR_SYS })
                    })
                }else {
                    res.json({ code: $.CODE_ERR_PASSWORD, msg: $.MSG_ERR_PASSWORD })
                }
            }else {
                res.json( { code: $.CODE_ERR_NO_USER, msg: $.MSG_ERR_NO_USER })
            }
        } ,err => {
            res.json({ code: $.CODE_ERR_SYS, msg: err })
        })
});
router.post('/register',(req, res, next) => {
   let username = req.body.username
   let password = req.body.password
   let nickname = req.body.nickname
   query(db_user.register.has_user,[username])
       .then(result => {
           if (result[0] > 0) {
               res.json({ code: $.CODE_ERR_REG_REPEAT, msg: $.MSG_ERR_REG_USERNAME })
           }else {
               return trans((conn) => {
                   return trans.query(conn, db_user.register.user, [username, password, new Date().getTime()])
                       .then(result => {
                           return trans.query(conn,db_user.login, [username])
                       }).then(result => {
                           return trans.query(conn,db_user.register.profile,[result[0].id, nickname])
                        })
               })
           }
       },err => {
           res.json({ code: $.CODE_ERR_SYS, msg: $.MSG_ERR_SYS})
   }).then(result =>{
       res.json({ code: $.CODE_SUCCESS, msg: $.MSG_SUCCESS_REG })
   }).catch(err => {
       res.json({ code: $.CODE_ERR_SYS, msg: $.MSG_ERR_REG_FAIL + err })
   })
});

router.post('/online',(req, res,next) => {
    let id = req.body.id
    query(db_user.online, [id])
        .then(result => {
            //通知好友我已经上线
        },err => {
            res.json({ code: $.CODE_ERR_SYS, msg: $.MSG_ERR_SYS })
        })
});
router.post('logout', (req, res, next) => {

});
router.post('/modify', (req, res, next) => {
    let id = req.body.id
    let key = req.body.key
    let value = req.body.value
    query(db_user.modify[key],[value, id])
        .then(result => {
            res.json({code: $.CODE_SUCCESS, msg:$.MSG_SUCCESS })
        }, err => {
            console.log(err)
            res.json({ code: $.CODE_ERR_SYS, msg: $.MSG_ERR_SYS })
        })
})

module.exports = router;

